package com.xgz.zhujie;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseDao<T> {
    //添加
    public int add(T t){
        //根据对象获取class对象
        Class<?> aClass = t.getClass();
        //获取表名————>类的注解
        TableName tabledAnnotation = aClass.getDeclaredAnnotation(TableName.class);
        String tablename = tabledAnnotation.value();
        //获取列名
        //1.获取该类所有的属性
        Field[] declaredFields = aClass.getDeclaredFields();
        List<String> columnNames = new ArrayList<>();
        List columnValues = new ArrayList<>();
        for (Field declaredField : declaredFields) {
            //2.获取该类的对应注解对象
            ColumnName ColumnNameAnnotation = declaredField.getDeclaredAnnotation(ColumnName.class);
            //3.获取列名————>注解对象的值
            String columnName = ColumnNameAnnotation.value();
            columnNames.add(columnName);
            //获取列的值
            declaredField.setAccessible(true);//暴力反射
            try {
                Object o = declaredField.get(t);
                columnValues.add("'"+o+"'");
                //System.out.println("列名是:"+columnName+"，对应的值：" + o);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        String columnValue = columnValues.toString().replace("[","(").replace("]",")");
        String columnName = columnNames.toString().replace("[","(").replace("]",")");
        String sql = "insert into " + tablename + columnName + "values" + columnValue;
        System.out.println("sql = " + sql);
        int result = -1;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test11?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true","root","xgz@123");
            PreparedStatement ps = con.prepareStatement(sql);
            result = ps.executeUpdate();
            con.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    //修改
    public int upd(T t){
        //根据对象获取class对象
        Class<?> aClass = t.getClass();
        //获取表名————>类的注解
        TableName tabledAnnotation = aClass.getDeclaredAnnotation(TableName.class);
        String tablename = tabledAnnotation.value();
        //获取列名
        //1.获取该类所有的属性
        Field[] declaredFields = aClass.getDeclaredFields();
        List<String> columnNames = new ArrayList<>();
        List columnValues = new ArrayList<>();
        for (Field declaredField : declaredFields) {
            //2.获取该类的对应注解对象
            ColumnName ColumnNameAnnotation = declaredField.getDeclaredAnnotation(ColumnName.class);
            //3.获取列名————>注解对象的值
            String columnName = ColumnNameAnnotation.value();
            columnNames.add(columnName);
            //获取列的值
            declaredField.setAccessible(true);//暴力反射
            try {
                Object o = declaredField.get(t);
                columnValues.add("'"+o+"'");

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        String sql = "update " + tablename + " set " + columnNames.get(1) + "=" + columnValues.get(1)+ "," + columnNames.get(2) + "="+ columnValues.get(2)  + "where " +columnNames.get(0)+"="+ columnValues.get(0);
        System.out.println("sql = " + sql);
        int result = -1;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test11?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true","root","xgz@123");
            PreparedStatement ps = con.prepareStatement(sql);
            result = ps.executeUpdate();
            con.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    //删除
    public int del(T t){
        //根据对象获取class对象
        Class<?> aClass = t.getClass();
        //获取表名————>类的注解
        TableName tabledAnnotation = aClass.getDeclaredAnnotation(TableName.class);
        String tablename = tabledAnnotation.value();
        //获取列名
        //1.获取该类所有的属性
        Field[] declaredFields = aClass.getDeclaredFields();
        List<String> columnNames = new ArrayList<>();
        List columnValues = new ArrayList<>();
        for (Field declaredField : declaredFields) {
            //2.获取该类的对应注解对象
            ColumnName ColumnNameAnnotation = declaredField.getDeclaredAnnotation(ColumnName.class);
            //3.获取列名————>注解对象的值
            String columnName = ColumnNameAnnotation.value();
            columnNames.add(columnName);
            //获取列的值
            declaredField.setAccessible(true);//暴力反射
            try {
                Object o = declaredField.get(t);
                columnValues.add("'"+o+"'");

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        String sql = "delete from " + tablename + " where " +columnNames.get(0)+"="+ columnValues.get(0);
        System.out.println("sql = " + sql);
        int result = -1;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test11?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true","root","xgz@123");
            PreparedStatement ps = con.prepareStatement(sql);
            result = ps.executeUpdate();
            con.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    //查询
    public int queryAll(T t){
        //根据对象获取class对象
        Class<?> aClass = t.getClass();
        //获取表名————>类的注解
        TableName tabledAnnotation = aClass.getDeclaredAnnotation(TableName.class);
        String tablename = tabledAnnotation.value();
        String sql = "select * from " + tablename;
        System.out.println("sql = " + sql);
        int result = -1;
        List<Map<String ,Object>> arrayList = new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test11?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true","root","xgz@123");
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            //获取列的属性和类型的信息
            ResultSetMetaData metaData = rs.getMetaData();
            //获取列的总数
            int columnCount = metaData.getColumnCount();
            while (rs.next()){
                Map<String , Object> map = new HashMap<>();
                //获取列值
                for (int i = 0; i < columnCount; i++) {
                    // 根据列的索引获取列名
                    String columnName = metaData.getColumnLabel(i+1);
                    //列值
                    Object columnValue = rs.getObject(i + 1);
                    //添加进Map里
                    map.put(columnName,columnValue);
                }
                arrayList.add(map);
            }
            System.out.println("id     name     money");
            for (Map<String, Object> map : arrayList) {
                System.out.println(
                                map.get("id")+"      "+
                                map.get("name")+"     "+
                                map.get("money"));
            }

            con.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
